package edu.mobbuzz.page.menu.rsschannel;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.RSSChannel;
import edu.mobbuzz.facade.RSSChannelFacade;
import edu.mobbuzz.messaging.RequestStream;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.page.menu.RSSChannelMenu;
import edu.mobbuzz.renderer.RSSChannelRenderer;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.string.ModeString;

/**
 *
 * @author irw
 */
public class RSSChannelPage extends Menu{
    private Form rssChannelForm;
    private Container rssChannelPanel;
    private Label menuTitle;
    private DefaultListModel rssChannelListModel;
    private List rssChannelList;    
    private Command backCmd;
    private Command unregCmd;
    private Command regCmd;
    private Command categoryCmd;
    private Command detailCmd;
    private RSSChannelFacade rssChannelFacade;
    
    public RSSChannelPage(){
        rssChannelForm = new Form();
        rssChannelPanel = new Container();        
        menuTitle = new Label(getMenuTitle());
        
        rssChannelListModel = new DefaultListModel(getRssChannelFacade().displayRSSChannelList());
        
        
        detailCmd = new Command("Detail") {
            public void actionPerformed(ActionEvent evt) {
                rssChannelDetail();
            }
        };
        
        regCmd = new Command("Subscribe RSS Channel") {
            public void actionPerformed(ActionEvent evt) {
               subscribeRSSChannel(ModeString.NEW);
            }
        };        
        
        
        unregCmd = new Command("Unsubscribe") {
            public void actionPerformed(ActionEvent evt) {
               unsubscribeRSSChannel();
            }
        };
        
        categoryCmd = new Command("Categories"){
            public void actionPerformed(ActionEvent evt){
                addToCategory();
            }
        };
        
        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                backToRSSChannelMenu();
            }
        };
        
    }
    public void showPage() {
        
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);    
        
        rssChannelPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));                 
        rssChannelPanel.addComponent(menuTitle);
        rssChannelPanel.addComponent(createList(rssChannelListModel, List.VERTICAL, new RSSChannelRenderer()));
        
        rssChannelForm.setLayout(new BorderLayout());       
        rssChannelForm.addComponent(BorderLayout.CENTER, rssChannelPanel);
        
        rssChannelForm.addCommand(backCmd);
        
        if(rssChannelListModel.getSize()!=0){
            rssChannelForm.addCommand(categoryCmd);
            rssChannelForm.addCommand(unregCmd);
           
            
        }
        
        rssChannelForm.addCommand(regCmd);
        
        if(rssChannelListModel.getSize()!=0){
            rssChannelForm.addCommand(detailCmd);
        }       
        
        rssChannelForm.setCommandListener(getActionlistener());
        rssChannelForm.setScrollableY(true);
        rssChannelForm.show();
    }
    

    private List createList(DefaultListModel rssChannelListModel, int orientation, ListCellRenderer renderer) {
        rssChannelList = new List(rssChannelListModel){
            public void keyPressed(int keyCode){
               gameActionPerformed(keyCode);
            }
        };        
        rssChannelList.getStyle().setBgTransparency(0);
        rssChannelList.getStyle().setBorder(Border.getEmpty());
        rssChannelList.setListCellRenderer(renderer);
        rssChannelList.setOrientation(orientation);
        return rssChannelList;
    }

    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);
        
        if(game == Display.GAME_FIRE){
            if(rssChannelListModel.getSize()==0){
                subscribeRSSChannel(ModeString.NEW);
            }else{
                rssChannelDetail();
            }
        }else if(game == Display.GAME_DOWN){            
            if(rssChannelList.getSelectedIndex()<(rssChannelList.size()-1)){
               rssChannelList.setSelectedIndex(rssChannelList.getSelectedIndex()+1);
            }else{
                rssChannelList.setSelectedIndex(0);
            }
           
        }else if(game == Display.GAME_UP){
            if(rssChannelList.getSelectedIndex()!=0){
                rssChannelList.setSelectedIndex(rssChannelList.getSelectedIndex()-1);
            }else{
                rssChannelList.setSelectedIndex(rssChannelList.size()-1);
            }           
        }
    }
    
    
    
    public void rssChannelDetail(){
        RSSChannelDetail rssChannelDetail = new RSSChannelDetail();
        rssChannelDetail.setExitCmd(getExitCmd());
        rssChannelDetail.setActionlistener(getActionlistener());
        rssChannelDetail.setRssChannel((RSSChannel)rssChannelList.getSelectedItem());
        rssChannelDetail.showPage();
    }
    
    public void subscribeRSSChannel(int mode) {
        RSSSubscriptionForm subscriptionForm = new RSSSubscriptionForm();
        subscriptionForm.setMode(mode);
        subscriptionForm.setExitCmd(getExitCmd());
        subscriptionForm.setActionlistener(getActionlistener());
        subscriptionForm.setMessaging(getMessaging());
        if(mode==ModeString.EDIT){
            subscriptionForm.setRssChannel((RSSChannel)rssChannelList.getSelectedItem());           
        }
        subscriptionForm.showPage();
    }
    
    public void unsubscribeRSSChannel(){
        TextArea body = new TextArea("Are you sure want to unsubscribe this rss ?", 2, 5);
        body.setGrowByContent(true);
        Command noCommand = new Command("No");
        Command yesCommand = new Command("Yes"){
            public void actionPerformed(ActionEvent evt) {
                RSSChannel channel = (RSSChannel)rssChannelList.getSelectedItem();
                //getMessaging().getUsername()+"@"+getMessaging().getDomain()
                StringBuffer request = RequestStream.sendRssRequest("unreg", channel.getTitle(), channel.getSource());
                getMessaging().sendMessage("news@mobbuzz.com",request.toString());
            }
        };
        Dialog.show("", body, new Command[]{noCommand, yesCommand}, Dialog.TYPE_CONFIRMATION, null,0,null);
               
    }
    
    
    public void addToCategory(){
        RSSChannelChoice rssChannelChoice = new RSSChannelChoice();
        rssChannelChoice.setExitCmd(getExitCmd());
        rssChannelChoice.setActionlistener(getActionlistener());
        rssChannelChoice.setRssChannel((RSSChannel)rssChannelList.getSelectedItem());
        rssChannelChoice.setIndex(rssChannelList.getSelectedIndex());
        rssChannelChoice.showPage();
    }
    
    public void backToRSSChannelMenu(){
        getRssChannelFacade().closeRSSChannelRS();
        RSSChannelMenu rssChannelMenu = new RSSChannelMenu();
        rssChannelMenu.setExitCmd(getExitCmd());
        rssChannelMenu.setActionlistener(getActionlistener());
        rssChannelMenu.showPage();
    }


    public RSSChannelFacade getRssChannelFacade() {
        if(rssChannelFacade==null){
            rssChannelFacade = new RSSChannelFacade();
        }
        return rssChannelFacade;
    }
    
    public String getMenuTitle() {
        return MenuString.RSSCHANNEL;
    }

}
